package com.lsk.netdisk.auth.helpers;

import com.lsk.netdisk.auth.authc.Authc;
import com.lsk.netdisk.auth.mappers.UserMapper;
import com.lsk.netdisk.auth.model.User;
import com.lsk.netdisk.common.redis.core.RedisComponent;
import com.lsk.netdisk.common.response.exception.HttpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserHelper {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private RedisComponent redisComponent;

	public void renameUser(String token, String newName){
		if(!Authc.isUserLogined(token)){
			throw new HttpException("Not login",403);
		}
		User user = Authc.getCurrentUser(token);
		if (userMapper.queryUserByName(newName) != null){
			throw new HttpException("User "+newName+" exists",500);
		}
		if (user.getName().equals(newName)){
			throw new HttpException("The old name as same as new name",500);
		}
		user.setName(newName);
		userMapper.updateUser(user);
	}

	public void rePassword(String token, String newPassword){
		if(!Authc.isUserLogined(token)){
			throw new HttpException("Not login",403);
		}
		User user = Authc.getCurrentUser(token);
		if (user.getPassword().equals(newPassword)){
			throw new HttpException("The old name as same as new name",500);
		}
		user.setPassword(newPassword);
		userMapper.updateUser(user);
	}
}
